#!/usr/bin/lua

ApmCsv={};

local function splist(str,reps)  
    local result = {};
    string.gsub(str, '[^' .. reps ..']+', function(w) table.insert(result, w) end );
    return result;
end

function ApmCsv.load(csv_text,with_header)
    local line = splist(csv_text, '\n\r');
    data = {};
    for i,v in ipairs(line) do  
        if with_header==false then
           -- 如果包括头部信息，应该被跳过
           if i>=2 then  
              local va = splist(v, ",");
              data[i-1]={};
              for j,b in ipairs(va) do  
                data[i-1][j] = b;
              end 
           end
        else
           -- 第一行就是数据 
           local va = splist(v, ",");
           data[i]={}; 
           for j,b in ipairs(va) do  
              data[i][j] = b;
           end
        end
    end
    return data;
end

return ApmCsv;
